package org.mtmi.ui.controls.midi;

import java.util.Collection;

import org.mtmi.ui.actions.Action;
import org.mtmi.ui.actions.IRetargetableAction;

public abstract class AbstractRetargettableMidiAction extends Action implements IRetargetableAction
{
	//might be IMidiControl or a list of IMidiControl
	protected Object target;
	
	public AbstractRetargettableMidiAction( IMidiControl c ) {
		super( PUSH );
		setTarget( c );
	}
	
	@Override
	/** @return true if object is an instance of IMidiControl or a collection of IMidiControl instances */
	public boolean isTargetHandled( Object c ) {
		if( c instanceof IMidiControl ) return true;
		if( c instanceof Collection ) {
			Collection<?> l  = (Collection<?>)c;
			for( Object o : l ) {
				if( !( o instanceof IMidiControl )  ) return false;
			}
			return l.size()>0;
		}
		return false;
	}
	
	@Override
	/** change target and call update() */
	public void setTarget( Object c ) {
		target = c;
		update();
	}
	
	@Override
	public Object getTarget() {
		return target;
	}

	@Override
	public boolean isEnabled() {
		if( target instanceof Collection ) {
			return ((Collection<?>)target).size()>0;
		}
		return target instanceof IMidiControl ;
	}

	@Override
	public boolean equals( Object obj ) {
		return obj!=null && obj.getClass() == getClass();//instanceof will cover subclasses like RemoveKeyboardOctaveAction
	}
	
}